<?php

class Zend_Controller_Action_Helper_Admin extends Zend_Controller_Action_Helper_Abstract
{
    public function direct()
    {
        return $this;
    }

    public function prepareStockParams($params, Zend_Form_Element_File $file)
    {
        $result = array();

        $result[Application_Model_DbTable_Stock::FIELD_PHOTO] = $params['photo'];
        if ($file->isUploaded()) {
            $extension = substr(strrchr($file->getFileName(null, false), '.'), 1);
            $fileName = uniqid() . '.' . $extension;
            $uploadPath = realpath(APPLICATION_PATH . '/../i/stock') . '/' . $fileName;

            $filterFileRename = new Zend_Filter_File_Rename(array(
                'target'    => $uploadPath,
                'overwrite' => true)
            );

            $file->addFilter($filterFileRename);

            if ($file->receive()) {
                $result[Application_Model_DbTable_Stock::FIELD_PHOTO] = $fileName;
            }
        }

        if ((int) $params['stock_id']) {
            $result[Application_Model_DbTable_Stock::FIELD_ID] = (int) $params['stock_id'];
        }

        if ($params['title']) {
            $result[Application_Model_DbTable_Stock::FIELD_TITLE] = $params['title'];
        }

        if ($params['desc']) {
            $result[Application_Model_DbTable_Stock::FIELD_DESC] = $params['desc'];
        }

        if (isset($params['is_active']) && $params['is_active'] === 'on') {
            $result[Application_Model_DbTable_Stock::FIELD_IS_ACTIVE] = 1;
        } else {
            $result[Application_Model_DbTable_Stock::FIELD_IS_ACTIVE] = 0;
        }

        return $result;
    }

    public function prepareUsefulParams($params)
    {
        $result = array();

        if ($params['useful_id']) {
            $result[Application_Model_DbTable_Useful::FIELD_ID] = $params['useful_id'];
        }

        if ($params['useful_title']) {
            $result[Application_Model_DbTable_Useful::FIELD_TITLE] = $params['useful_title'];
        }

        if ($params['desc']) {
            $result[Application_Model_DbTable_Useful::FIELD_DESC] = $params['desc'];
        }

        if (isset($params['is_active']) && $params['is_active'] === 'on') {
            $result[Application_Model_DbTable_Useful::FIELD_IS_ACTIVE] = 1;
        } else {
            $result[Application_Model_DbTable_Useful::FIELD_IS_ACTIVE] = 0;
        }

        return $result;
    }

    public function prepareGalleryParams($params, Zend_Form_Element_File $file)
    {
        $result = array();
        $result[Application_Model_DbTable_Gallery::FIELD_ID] = isset($params['photo_id']) ? $params['photo_id']: $params['photo_id'];
        $result[Application_Model_DbTable_Gallery::FIELD_DESC] = '';
        $result[Application_Model_DbTable_Gallery::FIELD_ALBUM] = isset($params['album_id']) && $params['album_id'] ? $params['album_id'] : 0;
        $result[Application_Model_DbTable_Gallery::FIELD_PATH] = isset($params['photo']) ? $params['photo']: $params['photo_path'];
        if ($file->isUploaded()) {
            $extension = substr(strrchr($file->getFileName(null, false), '.'), 1);
            $fileName = uniqid() . '.' . $extension;
            $uploadPath = realpath(APPLICATION_PATH . '/../i/gallery') . '/' . $fileName;

            $filterFileRename = new Zend_Filter_File_Rename(array(
                    'target'    => $uploadPath,
                    'overwrite' => true)
            );

            $file->addFilter($filterFileRename);

            if ($file->receive()) {
                $result[Application_Model_DbTable_Gallery::FIELD_PATH] = $fileName;
            }

            $this->_createThumb($uploadPath, 180, realpath(APPLICATION_PATH . '/../i/gallery/thumb') . '/' . $fileName);
        }

        return $result;
    }

    /**
     * ресайз картинки до размера width
     */
    private function _createThumb($realPath, $width, $thumbPath)
    {

        $info = getimagesize($realPath);

        if ($info['mime'] == 'image/png') {
            $src = imagecreatefrompng($realPath);
        } else if ($info['mime'] == 'image/jpeg') {
            $src = imagecreatefromjpeg($realPath);
        } else if ($info['mime'] == 'image/gif') {
            $src = imagecreatefromgif($realPath);
        } else {
            return false;
        }

        $size = array($info[0], $info[1]);
        $height = ($width / $size[1] ) * $size[0];
        $thumb = imagecreatetruecolor($width, $height);
        $new_size = array($width, $height);
        $src_pos = array(0,0);
        $new_size[0] = max($new_size[0], 1);
        $new_size[1] = max($new_size[1], 1);
        imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);
        return imagepng($thumb, $thumbPath);
    }

    public function prepareRentParams($params, Zend_Form_Element_File $file)
    {
        $result = array();

        if ($params['rent_id']) {
            $result[Application_Model_DbTable_Rent::FIELD_ID] = $params['rent_id'];
        }

        if ($params['title']) {
            $result[Application_Model_DbTable_Rent::FIELD_TITLE] = $params['title'];
        }

        if ($params['category']) {
            $result[Application_Model_DbTable_Rent::FIELD_CATEGORY] = $params['category'];
        }

        if ($params['price']) {
            $result[Application_Model_DbTable_Rent::FIELD_PRICE] = $params['price'];
        }
        if ($params['price_more']) {
            $result[Application_Model_DbTable_Rent::FIELD_PRICE_MORE] = $params['price_more'];
        }

        if ($params['bail']) {
            $result[Application_Model_DbTable_Rent::FIELD_BAIL] = $params['bail'];
        }

        if ($params['desc']) {
            $result[Application_Model_DbTable_Rent::FIELD_DESC] = $params['desc'];
        }

        if (isset($params['is_active']) && $params['is_active'] === 'on') {
            $result[Application_Model_DbTable_Rent::FIELD_IS_ACTIVE] = 1;
        } else {
            $result[Application_Model_DbTable_Rent::FIELD_IS_ACTIVE] = 0;
        }

        $result[Application_Model_DbTable_Rent::FIELD_PHOTO] = $params['photo'];

        if ($file->isUploaded()) {
            $extension = substr(strrchr($file->getFileName(null, false), '.'), 1);
            $fileName = uniqid() . '.' . $extension;
            $uploadPath = realpath(APPLICATION_PATH . '/../i/rent') . '/' . $fileName;

            $filterFileRename = new Zend_Filter_File_Rename(array(
                    'target'    => $uploadPath,
                    'overwrite' => true)
            );

            $file->addFilter($filterFileRename);

            if ($file->receive()) {
                $result[Application_Model_DbTable_Rent::FIELD_PHOTO] = $fileName;
            }

            $this->_createThumb($file, 180, realpath(APPLICATION_PATH . '/../i/rent/thumb') . '/' . $fileName);
        }

        return $result;
    }

    public function prepareRentCategoryParams($params, Zend_Form_Element_File $file)
    {
        $result = array();

        if ($params['category_id']) {
            $result[Application_Model_DbTable_RentCategory::FIELD_ID] = $params['category_id'];
        }

        if ($params['title']) {
            $result[Application_Model_DbTable_RentCategory::FIELD_TITLE] = $params['title'];
        }

        if ($params['desc']) {
            $result[Application_Model_DbTable_RentCategory::FIELD_DESC] = $params['desc'];
        }

        if ($params['album_id']) {
            $result[Application_Model_DbTable_RentCategory::FIELD_ALBUM] = $params['album_id'];
        } else {
            $result[Application_Model_DbTable_RentCategory::FIELD_ALBUM] = 0;
        }

        if ($params['category_4pu']) {
            $result[Application_Model_DbTable_RentCategory::FIELD_4PU] = $params['category_4pu'];
        }

        $result[Application_Model_DbTable_RentCategory::FIELD_ICON] = $params['icon'];

        if ($file->isUploaded()) {
            $fileInfo = $file->getFileInfo();
            $filePath = $fileInfo['icon']['tmp_name'];
            $extension = substr(strrchr($file->getFileName(null, false), '.'), 1);
            $fileName = uniqid() . '.' . $extension;
            $uploadPath = realpath(APPLICATION_PATH . '/../i/rentcat') . '/' . $fileName;
            $this->_createThumb($filePath, 180, $uploadPath);
            $result[Application_Model_DbTable_RentCategory::FIELD_ICON] = $fileName;
        }

        return $result;
    }

    public function prepareGalleryAlbum($params)
    {
        $result = array();

        if ((int) $params['album_id']) {
            $result[Application_Model_DbTable_GalleryAlbums::FIELD_ID] = (int) $params['album_id'];
        }

        if ($params['album_title']) {
            $result[Application_Model_DbTable_GalleryAlbums::FIELD_TITLE] = $params['album_title'];
        }

        if ($params['album_icon']) {
            $result[Application_Model_DbTable_GalleryAlbums::FIELD_ICON] = $params['album_icon'];
        }

        return $result;
    }
}